﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlTypes;
using Spring.Data;
using Spring.Data.Generic;
using Spring.Data.Common;
using Spring.Data.Objects.Generic;

namespace Ordering
{
    public enum EventAction
    {
        AddShop, AddDish
        /*
        CreateEngagement, UpdateEngagement, SyncEngagementFromPMS,
        CreateCustomer, UpdateCustomer, DeleteCustomer,
        CreateBank, UpdateBank, DeleteBank,
        CreateConfirm, UpdateConfirm, ChangeConfirmStatus, ChangeConfirmStatusForIncomplete, DeleteConfirm, SupersedeConfirm, SupersedeConfirmByBatch, DeliverFailed,
        CreateConfirmDoc, DeleteConfirmDoc, ReplaceConfirmDoc,
        UploadFromBank, UploadFromClient, UploadFromBankIncomplete,
        CreateConfirmMailing,
        SendMail, SendRemindRollForwardMail, SendMailForEng, SendMailRecvFromBank, SendMailForDelivered, SendMailForRollForward, SendMailForSuperseded, SendMailForIncompleted,
        RollForward, CreateConfirmByRollForward,
        CreateConfirmRemark, UpdateRemarkInConfirm
        */
    }

    public class EventLog
    {
        private int _ID;
        private string _EventID;
        private string _ActionType;
        private DateTime _ActionTime;
        private string _Operator;
        private string _Remark;

        #region properties
        public int ID
        {
            get { return this._ID; }
            set { this._ID = value; }
        }

        public string EventID
        {
            get { return this._EventID; }
            set { this._EventID = value; }
        }

        public string ActionType
        {
            get { return this._ActionType; }
            set { this._ActionType = value; }
        }

        public DateTime ActionTime
        {
            get { return this._ActionTime; }
            set { this._ActionTime = value; }
        }

        public string Operator
        {
            get { return this._Operator; }
            set { this._Operator = value; }
        }

        public string Remark
        {
            get { return this._Remark; }
            set { this._Remark = value; }
        }
        #endregion

        public EventLog() { }

        public EventLog(string id, string eType, string oper, string remark)
        {
            this._EventID = id;
            this._ActionType = eType;
            this._ActionTime = DateTime.Now;
            this._Operator = oper;
            this._Remark = remark;
        }
    }

    public static class EventLogDAO
    {
        private static AdoTemplate template = AppConfigurer.AdoTemplate;

        private static string strBase = "select Id, Event_Id, Action_Type, Action_Time, Operator, Remark  "
            + " from Event_Log ";

        #region static methods
        public static IList<EventLog> ListObjects(string eventId)
        {
            //Execute Query 
            string strSel = strBase + " where Event_Id = @eventid order by Id desc ";
            IDbParameters p = template.CreateDbParameters();
            p.Add("eventid", DbType.String).Value = eventId;

            IList<EventLog> list = template.QueryWithRowMapperDelegate(CommandType.Text, strSel,
                new RowMapperDelegate<EventLog>(MapRow), p);

            return list;
        }

        public static IList<EventLog> ListObjects(string eventId, string actType)
        {
            //Execute Query 
            string strSel = strBase + " where Event_Id = @eventid and Action_Type=@actType order by Id desc ";
            IDbParameters p = template.CreateDbParameters();
            p.Add("eventid", DbType.String).Value = eventId;
            p.Add("actType", DbType.String).Value = actType;

            IList<EventLog> list = template.QueryWithRowMapperDelegate(CommandType.Text, strSel,
                new RowMapperDelegate<EventLog>(MapRow), p);

            return list;
        }

        public static IList<EventLog> ListObjects(DateTime dtStart, DateTime dtend)
        {
            //Execute Query 
            string strSel = strBase + " where Action_Time > @startTime "
                + " and Action_Time < @endTime "
                + " order by Action_Time desc ";

            IDbParameters p = template.CreateDbParameters();
            p.Add("startTime", DbType.DateTime).Value = dtStart;
            p.Add("endTime", DbType.DateTime).Value = dtend;

            IList<EventLog> list = template.QueryWithRowMapperDelegate(CommandType.Text, strSel,
                new RowMapperDelegate<EventLog>(MapRow), p);

            return list;
        }

        public static void newObject(EventLog obj)
        {
            string strUpd = "insert into Event_Log(Event_Id, Action_Type, Action_Time, Operator, Remark) "
                + " values(@id, @actionType, @actionTime, @actionBy, @remark)";

            IDbParameters p = template.CreateDbParameters();
            p.Add("id", DbType.String).Value = obj.EventID;
            p.Add("actionType", DbType.String).Value = obj.ActionType;
            p.Add("actionTime", DbType.DateTime).Value = obj.ActionTime;
            p.Add("actionBy", DbType.String).Value = obj.Operator;
            if (obj.Remark.Length > 2000)
                obj.Remark = obj.Remark.Substring(0, 2000);
            p.Add("remark", DbType.String).Value = obj.Remark;

            template.ExecuteNonQuery(CommandType.Text, strUpd, p);
        }
        #endregion

        #region mapper
        private static EventLog MapRow(IDataReader results, int rowNumber)
        {
            EventLog obj = new EventLog();

            obj.ID = results.GetInt32(0);
            obj.EventID = results.GetString(1);
            obj.ActionType = results.GetString(2);
            obj.ActionTime = results.GetDateTime(3);
            obj.Operator = results.GetString(4);
            obj.Remark = results.GetString(5);

            return obj;
        }
        #endregion
    }

    public static class LogUtil
    {
        public static void LogEvent(string EventID, EventAction evtType, string oper, string remark)
        {
            EventLog log = new EventLog(EventID, evtType.ToString(), oper, remark);
            EventLogDAO.newObject(log);
        }

        public static void LogEvent(string EventID, string evt, string oper, string remark)
        {
            EventLog log = new EventLog(EventID, evt, oper, remark);
            EventLogDAO.newObject(log);
        }

        public static void LogEvent(string EventID, EventAction evtType, string oper, IDictionary<string, string> dicChange)
        {
            string strRemark = "na";

            if (dicChange != null && dicChange.Count != 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (KeyValuePair<string, string> kv in dicChange)
                    sb.Append("," + kv.Key + ":" + kv.Value);

                sb.Remove(0, 1);  //Remove the first ","

                strRemark = sb.ToString();
            }

            LogEvent(EventID, evtType, oper, strRemark);
        }

        public static void LogAppEvent(string oper, string remark)
        {
            EventLog log = new EventLog("00000", "AppEvent", oper, remark);
            EventLogDAO.newObject(log);
        }
    }
}

